﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HelperPrototype.OtherForms
{
    public partial class ThemGroupUserForm : Form
    {
        private int ID = -1;
        private int isEdit = 1;

        public ThemGroupUserForm()
        {
            InitializeComponent();
        }

        public ThemGroupUserForm(int groupItemID,int edit)
        {
            InitializeComponent();
            isEdit = edit;
            ID = groupItemID;
        }

        private void ThemGroupUserForm_Load(object sender, EventArgs e)
        {
            loadData();
        }

        private void loadData()
        {
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();

            // fill check list quyen
            clbQuyen.Items.Clear();
            foreach (_DB.QuyenHan q in dc.QuyenHans)
            if (q.deleted == false)
                clbQuyen.Items.Add(q);

            if (ID < 0)
                return;

            // fill info
            _DB.NhomNguoiDung nhom = dc.NhomNguoiDungs.Single(x => x.NhomNguoiDung_ID == ID);

            txtName.Text = nhom.TenNhom;
            txtNote.Text = nhom.GhiChu;
            txtStatus.Text = nhom.TrangThai;
            if (isEdit==0)
            {
                txtName.ReadOnly = true;
                txtNote.ReadOnly = true;
                txtStatus.ReadOnly = true;
                clbQuyen.Enabled = false;
            }

            foreach (_DB.NhomNguoiDung_QuyenHan nq in nhom.NhomNguoiDung_QuyenHans)
            {
                int i = clbQuyen.Items.IndexOf(nq.QuyenHan);
                clbQuyen.SetItemChecked(i, true);
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            // check
            if (isEdit == 0)
            {
                this.Close();
                return;
            }

            if (txtName.Text.Trim().Length == 0)
            {
                MessageBox.Show("Cần điền đầy đủ thông tin");
                return;
            }

            // go
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            _DB.NhomNguoiDung nhom;
            if (ID < 0)
                nhom = new _DB.NhomNguoiDung();
            else
                nhom = dc.NhomNguoiDungs.Single(x => x.NhomNguoiDung_ID == ID);
           
            nhom.TenNhom = txtName.Text.Trim();
            nhom.GhiChu = txtNote.Text;
            nhom.TrangThai = txtStatus.Text;

            dc.NhomNguoiDung_QuyenHans.DeleteAllOnSubmit(nhom.NhomNguoiDung_QuyenHans);
            foreach (object i in clbQuyen.CheckedItems)
            {
                _DB.QuyenHan quyen = (_DB.QuyenHan)i;
                _DB.NhomNguoiDung_QuyenHan nq = new _DB.NhomNguoiDung_QuyenHan();
                nq.QuyenHanID = quyen.QuyenHan_ID;
                nhom.NhomNguoiDung_QuyenHans.Add(nq);
            }

            if (ID < 0)
                dc.NhomNguoiDungs.InsertOnSubmit(nhom);
            dc.SubmitChanges();
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
